<template>
    <div>
        <div class="acea-row row-middle input-box margin1">
            <img :src="leftIcon" alt="left-icon" class="left-icon">
            <input type="tel" placeholder="手机号码" v-model='mobile' class="input-tel">
        </div>
        <div class="acea-row row-middle input-box margin2">
            <img :src="leftIcon1" alt="left-icon" class="left-icon">
            <input type="number" placeholder="短信验证码" v-model='code' class="input-tel input-code">
            <Button  class="get-code" size="mini" :disabled="disabled" @click="onSendCode">{{text}}</Button>
        </div>
        <Button text="立即绑定" color="#0DCDB5" class="bind-btn" @click="bindingMobile"/>
    </div>
</template>

<script>
    import { Button,Toast } from 'vant';
    import SendVerifyCode from "../mixins/SendVerifyCode";
    import localStorage from "../utils/store/localStorage";
    import {getSMSCode} from "../api/public";
    import {isWeixin} from "../utils";
    import {checkWXOpenId, registerActivityLead, wechatUserBindMobile} from "../api/user";
    import md5 from 'md5'
    import {mapActions} from "vuex";
    const loadImg = name => require(`@/assets/images/${name}.png`);
    export default {
        mixins:[SendVerifyCode],
        name: "BindingMobile",
        components:{
          Button
        },
        data() {
            return {
                mobile: '',
                leftIcon: loadImg('mobile'),
                leftIcon1:loadImg('verify_code'),
                code:''
            }
        },
        methods:{
            ...mapActions(['USERINFO']),
            onSendCode(){
                if (this.mobile.length !== 11 ) return Toast('请输入正确的手机号码！');
                getSMSCode(this.mobile,'Register_Code').then(res => {
                    Toast(res.msg);
                    if (res.code) return this.sendCode()
                })
            },
            setUserInfo(userId){
                this.USERINFO(userId).then(data => {
                    localStorage.set("user", data);
                    const login_back_url = localStorage.get("login_back_url");
                    const smId = localStorage.get('smId')
                    registerActivityLead(smId,userId).then(res => {
                        if (res.code) {
                            if (login_back_url) {
                                this.$router.push(login_back_url)
                            } else {
                                this.$router.push('/')
                            }
                        }else {
                            // return false
                            Toast('非法用户注册！');
                            this.$router.push('/')
                        }
                    })
                }).catch(err => {
                    Toast(err);
                })
            },
            bindingMobile(){
                if (this.mobile.length !== 11 ) return Toast('请输入正确的手机号码！');
                if (isWeixin()){
                    const wxUser = localStorage.get("wxUser");
                    const openid = localStorage.get("openid");
                    wechatUserBindMobile(this.mobile,openid,this.code,wxUser.headimgurl,wxUser.nickname).then(res => {
                        if (res.code) {
                            const openid = localStorage.get("openid");
                            const wxUser = localStorage.get("wxUser");
                            checkWXOpenId(openid, wxUser.headimgurl, wxUser.nickname).then(
                                response => {
                                    if (response.code) {
                                        const tokenStr = response.data.jwttoken.split(".");
                                        const t = md5(tokenStr[1] + response.data.jwtrandomKey);
                                        const jwttoken = response.data.jwttoken;
                                        localStorage.set("token", jwttoken);
                                        localStorage.set("sign", t);
                                        if (localStorage.get("sign")) {
                                            this.setUserInfo(res.data);
                                        }
                                    }
                                }
                            );
                        } else {
                            Toast(res.msg);
                        }
                    });
                }else {
                    return Toast('请在微信客户端内打开链接')
                }
            }
        }
    }
</script>

<style scoped lang="scss">
    .input-box {
        width: 335px;
        height: 50px;
        background: rgba(255, 255, 255, 1);
        border-radius: 8px;
        margin-left: auto;
        margin-right: auto;

        .left-icon {
            width: 22px;
            height: 22px;
            margin-left: 18px;
            margin-right: 20px;
        }
        .input-tel{
            height: 30px;
            width: 70%;
            font-size:16px;
        }
        .input-tel::-webkit-input-placeholder{
            color:#BCBFCE;
            font-size:16px;
            font-weight:400;
        }
        .input-tel::-moz-placeholder{   /* Mozilla Firefox 19+ */
            color:#BCBFCE;
            font-size:16px;
            font-weight:400;
        }
        .input-tel:-moz-placeholder{    /* Mozilla Firefox 4 to 18 */
            color:#BCBFCE;
            font-size:16px;
            font-weight:400;
        }
        .input-tel:-ms-input-placeholder{  /* Internet Explorer 10-11 */
            color:#BCBFCE;
            font-size:16px;
            font-weight:400;
        }
    }
    .margin1{
        margin-top: 40px;
    }
    .margin2{
        margin-top: 20px;
        .get-code{
            font-size:14px;
            font-weight:400;
            flex: 1;
            color:rgba(13,205,181,1);
            margin-right: 20px;
        }
        .input-code{
            width: 50%;
            font-size:16px;
        }
    }
    .bind-btn{
        width:305px;
        height:50px;
        background:rgba(13,205,181,1);
        border-radius:6px;
        display: block;
        margin: 50px auto 0;
    }
</style>
